package cz.cvut.sefcijak.zatacka;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Context;
import android.graphics.*;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View.OnTouchListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: sefcijak
 * Date: 25.3.13
 * Time: 12:01
 */

public class KresliView extends View implements View.OnTouchListener{
    GameActivity GA;
    private static final String TAG = "DEBug";
    public static Canvas canvas;
    public static Context contex;

    public static Bitmap bitmapaCista = null;
    public static Bitmap bitmapa = null;
    private Had aktualniHad;

    public Bod pulkaPlochy = Bod.Deleno(GA.maxBod,2);

    public KresliView(Context context) {
        super(context);
        contex = getContext();
        canvas = new Canvas();

        setFocusable(true);
        setFocusableInTouchMode(true);

        bitmapaCista = Bitmap.createBitmap(GA.maxBod.x, GA.maxBod.y, Bitmap.Config.RGB_565);

        this.setOnTouchListener(this);
    }
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        Log.d(TAG,"TUK");
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                GA.naKlik(new Bod((int)event.getX(),(int)event.getY()));
                return true;
                //break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            default:
                break;
        }
        return false;
    }
    @Override
    public void onDraw(Canvas canvas) {
        //TODO: onDrow → if(!GA.zapnuto)
        if(!GA.zapnuto || bitmapa == null)
            bitmapa = bitmapaCista;

        //vykresleni bitmapy
        canvas.drawBitmap(bitmapa,new Matrix(getMatrix()),new Paint());

        //cara pres pulku
        Paint pulka = new Paint();
        pulka.setColor(Color.WHITE);
        pulka.setStrokeWidth(1);
        pulka.setStyle(Paint.Style.STROKE);
        canvas.drawLine((float) 0, (float) pulkaPlochy.y, (float) GA.maxBod.x, (float) pulkaPlochy.y, pulka);

        //tex na plose
        canvas.drawText("Do prava",(float)pulkaPlochy.x,(float)pulkaPlochy.y/2,pulka);
        canvas.drawText("Do leva",(float)pulkaPlochy.x,(float)pulkaPlochy.y/2+pulkaPlochy.y,pulka);

    }
    public void pridejBod(Had had) {
        aktualniHad = had;
        kontrolaKruh(had.novyBod,had.smer);
        kresliKruh(had.novyBod,had.nastaveni.getColor());
        //had1.listBodu.add(point);
        invalidate();
    }
    public void pridejBod(Had had, Bod bod) {
        kontrolaKruh(bod,had.smer);
        //had1.listBodu.add(bod);
        kresliKruh(bod,had.nastaveni.getColor());
        invalidate();
    }
    public void pridejPrekazku(Bod bod){
        Bod vychozi = bod;
        kresliKruh(bod,Color.RED);
        kresliKruh(new Bod(bod.x+5,bod.y),Color.RED);
        kresliKruh(new Bod(bod.x-5,bod.y),Color.RED);
        kresliKruh(new Bod(bod.x,bod.y+5),Color.RED);
        kresliKruh(new Bod(bod.x,bod.y-5),Color.RED);
        kresliKruh(new Bod(bod.x+5,bod.y+5),Color.RED);
        kresliKruh(new Bod(bod.x+5,bod.y-5),Color.RED);
        kresliKruh(new Bod(bod.x-5,bod.y+5),Color.RED);
        kresliKruh(new Bod(bod.x-5,bod.y-5),Color.RED);
    }
    private void kresliKruh(Bod stred,int barva){
        Bod point = new Bod();
        point.x = stred.x;
        point.y = stred.y;

        bitmapa.setPixel(point.x,point.y,Color.YELLOW);
        bitmapa.setPixel(point.x-1,point.y,barva);
        bitmapa.setPixel(point.x+1,point.y,barva);
        bitmapa.setPixel(point.x,point.y-1,barva);
        bitmapa.setPixel(point.x,point.y+1,barva);
        bitmapa.setPixel(point.x+1,point.y+1,barva);
        bitmapa.setPixel(point.x+1,point.y-1,barva);
        bitmapa.setPixel(point.x-1,point.y+1,barva);
        bitmapa.setPixel(point.x-1,point.y-1,barva);
        bitmapa.setPixel(point.x+2,point.y,barva);
        bitmapa.setPixel(point.x-2,point.y,barva);
        bitmapa.setPixel(point.x,point.y+2,barva);
        bitmapa.setPixel(point.x,point.y-2,barva);
        bitmapa.setPixel(point.x+3,point.y,barva);
        bitmapa.setPixel(point.x-3,point.y,barva);
        bitmapa.setPixel(point.x,point.y+3,barva);
        bitmapa.setPixel(point.x,point.y-3,barva);
        bitmapa.setPixel(point.x+1,point.y+2,barva);
        bitmapa.setPixel(point.x+2,point.y+1,barva);
        bitmapa.setPixel(point.x-1,point.y-2,barva);
        bitmapa.setPixel(point.x-2,point.y-1,barva);
        bitmapa.setPixel(point.x-1,point.y+2,barva);
        bitmapa.setPixel(point.x-2,point.y+1,barva);
        bitmapa.setPixel(point.x+1,point.y-2,barva);
        bitmapa.setPixel(point.x+2,point.y-1,barva);

        //invalidate();
    }

    //TODO: kontrolaKruh → void -> boolean
    private void kontrolaKruh(Bod stred,Had.Otoceni smer){
        Bod point = new Bod();
        point.x = stred.x;
        point.y = stred.y;

        if((smer == Had.Otoceni.JEDNA) || (smer == Had.Otoceni.DVA)){
            kontrolaSmer(stred,Smer.NAHORU);
            kontrolaSmer(stred,Smer.DOPRAVA);            
        }else
        if(smer == Had.Otoceni.TRI){
            //kontrolaSmer(stred,Smer.NAHORU);
            //kontrolaSmer(stred,Smer.DOLU);
            kontrolaSmer(stred,Smer.DOPRAVA);
        }else
        if((smer == Had.Otoceni.CTIRI) || (smer == Had.Otoceni.PET)){
            kontrolaSmer(stred,Smer.DOLU);
            kontrolaSmer(stred,Smer.DOPRAVA);
        }else
        if(smer == Had.Otoceni.SEST){
            //kontrolaSmer(stred,Smer.DOLEVA);
            //kontrolaSmer(stred,Smer.DOPRAVA);
            kontrolaSmer(stred,Smer.DOLU);
        }else
        if((smer == Had.Otoceni.SEDM) || (smer == Had.Otoceni.OSM)){
            kontrolaSmer(stred,Smer.DOLU);
            kontrolaSmer(stred,Smer.DOLEVA);
        }else
        if(smer == Had.Otoceni.DEVET){
           // kontrolaSmer(stred,Smer.NAHORU);
            kontrolaSmer(stred,Smer.DOLEVA);
            //kontrolaSmer(stred,Smer.DOLU);
        }else
        if((smer == Had.Otoceni.DESET) || (smer == Had.Otoceni.JEDENACT)){
            kontrolaSmer(stred,Smer.DOLEVA);
            kontrolaSmer(stred,Smer.NAHORU);
        }else
        if(smer == Had.Otoceni.DVANACT){
            kontrolaSmer(stred,Smer.NAHORU);
            //kontrolaSmer(stred,Smer.DOPRAVA);
            //kontrolaSmer(stred,Smer.DOLEVA);
        }
    }
    private enum Smer{
        NAHORU,
        DOLU,
        DOPRAVA,
        DOLEVA,

    }
    private void kontrolaSmer(Bod point,Smer smer){
        switch (smer)
        {
            case NAHORU:
                if(
                    (bitmapa.getPixel(point.x,point.y+4) != Color.BLACK)   ||
                    //(bitmapa.getPixel(point.x,point.y+2) != Color.BLACK)   ||
                    (bitmapa.getPixel(point.x+1,point.y+4) != Color.BLACK) ||
                    (bitmapa.getPixel(point.x-1,point.y+4) != Color.BLACK)
                )
                    jePlno(point,smer);
                break;
            case DOLU:
                if(
                    (bitmapa.getPixel(point.x,point.y-4) != Color.BLACK)   ||
                    //(bitmapa.getPixel(point.x,point.y-2) != Color.BLACK)   ||
                    (bitmapa.getPixel(point.x+1,point.y-4) != Color.BLACK) ||
                    (bitmapa.getPixel(point.x-1,point.y-4) != Color.BLACK)
                 )
                    jePlno(point,smer);
                break;
            case DOLEVA:
                if(
                    (bitmapa.getPixel(point.x-4,point.y) != Color.BLACK)   ||
                    //(bitmapa.getPixel(point.x-2,point.y) != Color.BLACK)   ||
                    (bitmapa.getPixel(point.x-4,point.y+1) != Color.BLACK) ||
                    (bitmapa.getPixel(point.x-4,point.y-1) != Color.BLACK)
                  )
                    jePlno(point,smer);
                break;
            case DOPRAVA:
                if(
                    (bitmapa.getPixel(point.x+4,point.y) != Color.BLACK)   ||
                    //(bitmapa.getPixel(point.x+2,point.y) != Color.BLACK)   ||
                    (bitmapa.getPixel(point.x+4,point.y+1) != Color.BLACK) ||
                    (bitmapa.getPixel(point.x+4,point.y-1) != Color.BLACK)
                        )
                    jePlno(point,smer);
                break;
        }
    }
    private void jePlno(Bod bod,Smer smer){
        Log.i(TAG, "Trefa do barvy");
        //GA.tak2 = System.currentTimeMillis();
        GA.zapnuto = false;
        //throw new ExHad(aktualniHad,bod);
        int sekundy = (int)System.currentTimeMillis()-GA.tak1;
        sekundy = sekundy/1000;
        GA.zobrazDialog(aktualniHad.nazev +" se srazil s překážkou. Doba hraní je " + sekundy + " s");
        //GA.zobrazDialog(aktualniHad.nazev +" se srazil na (" + bod.toString() + ") ve směru "+ smer.toString());

    }

    public static void pause() {
        bitmapa = bitmapaCista;
        GameActivity.zapnuto = true;

    }

    public void resume() {
        bitmapa = bitmapaCista;
        GameActivity.zapnuto = true;
    }
}